/**
 * Copyright (c) 2017,安徽皖通科技股份有限公司 All rights reserved.
 *
 * @data:2017年10月11日
 * @author:MaShibo
 */
package com.wttech.dzzf.process.util;

import com.wtkj.vo.dzzf.share.staticvalue.CardStaticValue;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.regex.Pattern;

import static com.wtkj.vo.dzzf.share.staticvalue.CertifiType.HKANDMACAOCARD;
import static com.wttech.dzzf.process.constant.UserConstants.OLD_OWNERTYPE_COMPANY;
import static com.wttech.dzzf.process.constant.UserConstants.OLD_OWNERTYPE_USER;
import static com.wttech.dzzf.process.util.UserInfoStaticValue.CertifiType.*;

/**
 * @author MaShibo
 * @date 2017年10月11日
 */
public class BusinessUtils {

	private static final Logger LOGGER = LoggerFactory.getLogger(BusinessUtils.class);

	private static final String[] SEARCHES = new String[]{"$", "%", "^", "!", "~", "@", "+",
			"〇", " ", "!", "?", "？", ";", "`", "\\", "‘", "’", "{", "}", "。", "\u007F", "'", ":", ".", "\uE863", " "};

	private static final String NAME = "^[\\u4e00-\\u9fa5]+";

	private static String[] replaces = new String[SEARCHES.length];

	static {
		Arrays.fill(replaces, StringUtils.EMPTY);
	}

	private BusinessUtils() {
	}

	public static Integer convertUserIdType(Integer certifiType, Integer ownerType) {

		if (certifiType == null) {
			return ownerType == OLD_OWNERTYPE_USER ? 199 : 299;
		}

		StringBuilder cert = new StringBuilder();
		//个人用户
		if (ownerType == OLD_OWNERTYPE_USER) {
			cert.append("10");
			switch (certifiType) {
				case 0:
					// 身份证
					cert.append("1");
					break;
				case 1:
					// 军官证
					cert.append("5");
					break;
				case 2:
					// 护照
					cert.append("2");
					break;
				case 3:
					// 港澳居民来往内地通行证
					cert.append("3");
					break;
				case 4:
					// 临时身份证->身份证
					cert.append("1");
					break;
				case 6:
					// 台湾居民来往大陆通行证
					cert.append("4");
					break;
				case 7:
					// 港澳居民来往内地通行证
					cert.append("3");
					break;
				case 5:
					// 武警警察身份证
					cert.append("6");
					break;
				default:
					return 199;
			}
			//单位用户
		} else if (ownerType == OLD_OWNERTYPE_COMPANY) {
			cert.append("20");
			switch (certifiType) {
				//组织机构代码证
				case 101:
					cert.append("2");
					break;
				//统一社会信用代码
				case 102:
					cert.append("1");
					break;
				//营业执照
				case 103:
					cert.append("3");
					break;
				//事业单位法人证书
				case 104:
					cert.append("4");
					break;
				//社会团体法人登记证书
				case 105:
					cert.append("5");
					break;
				//律师事务所执业许可证
				case 106:
					cert.append("6");
					break;
				default:
					return 299;
			}
		} else {
			LOGGER.warn("错误的用户类型【{}】", ownerType);
			return 199;
		}

		return Integer.valueOf(cert.toString());
	}


	public static boolean checkUserIdType(Integer certifiType, String certifiNo) {
		if (isCertNoGroupOne(certifiType)) {
			return DataValidationCheck.checkCertifiNoOne(certifiNo);
		} else if (isCertNoGroupTwo(certifiType)) {
			return DataValidationCheck.checkCertifiNoTwo(certifiNo);
		} else if (certifiType == IDCARD) {
			return DataValidationCheck.checkIdCard(certifiNo);
		}
		return true;
	}

	private static boolean isCertNoGroupOne(Integer idType) {
		return idType != null
				&& (idType == PASSPORT || idType == GA_PERMIT || idType == T_PERMIT || idType == SOCIAL_CREDIT
				|| idType == ORGANIZATION || idType == BUSINESS_LICENSE || idType == LAWFIRM_ORGANIZATION);
	}

	private static boolean isCertNoGroupTwo(Integer idType) {
		return idType != null && (idType == ARMED_POLICE || idType == OFFICER || idType == PUBLIC_INSTITUTION
				|| idType == SOCIAL_ORGANIZATION);
	}

	/**
	 * 全角转半角 + 替换特殊符号
	 *
	 * @param original 待处理字符串
	 * @return 处理后字符串
	 */
	public static String regularString(String original) {
		if (StringUtils.isBlank(original)) {
			return StringUtils.EMPTY;
		}
		//去除两边空格 + 全角转半角 + 转换为大写 + 替换特殊字符
		return StringUtils.replaceEach(StringUtils.upperCase(convertToDBC(StringUtils.trim(original))), SEARCHES, replaces);
	}

	public static boolean containsChineseWords(String original) {
		return Pattern.matches(NAME, original);
	}


	/**
	 * 转换客服合作机构类型
	 *
	 * @param agencyType dzzf合作机构类型
	 * @return 部中心类型
	 */
	public static Integer convertChannelType(Integer agencyType) {
		if (agencyType != null) {
			switch (agencyType) {
				case 11:
					return 1;
				case 12:
					return 4;
				case 13:
					return 2;
				case 14:
					return 3;
				default:
					break;
			}
		}
		return 0;
	}

	/**
	 * 全角转半角
	 *
	 * @param input 全角字符串输入
	 * @return 半角字符串输出
	 */
	private static String convertToDBC(String input) {

		if (StringUtils.isBlank(input)) {
			return StringUtils.EMPTY;
		}

		char c[] = input.toCharArray();
		for (int i = 0; i < c.length; i++) {
			if (c[i] == '\u3000') {
				c[i] = ' ';
			} else if (c[i] > '\uFF00' && c[i] < '\uFF5F') {
				c[i] = (char) (c[i] - 65248);

			}
		}
		return new String(c);
	}

	/**
	 * 证件类型是否为单位用户类型
	 *
	 * @param certifiType 证件类型
	 * @return 是否单位用户
	 */
	public static boolean isCompanyCertifiType(int certifiType) {
		return certifiType == ORGANIZATION || certifiType == SOCIAL_CREDIT || certifiType == BUSINESS_LICENSE ||
				certifiType == PUBLIC_INSTITUTION || certifiType == SOCIAL_ORGANIZATION || certifiType == LAWFIRM_ORGANIZATION;
	}

	/**
	 * 证件类型是否为个人用户类型
	 *
	 * @param certifiType 证件类型
	 * @return 是否个人用户
	 */
	public static boolean isPersonCertifiType(int certifiType) {
		return certifiType == UserInfoStaticValue.CertifiType.IDCARD || certifiType == OFFICER || certifiType == UserInfoStaticValue.CertifiType.PASSPORT
				|| certifiType == GA_PERMIT || certifiType == TEMP_IDCARD || certifiType == ARMED_POLICE || certifiType == T_PERMIT || certifiType == HKANDMACAOCARD;

	}

	/**
	 * 确定卡品牌
	 *
	 * @param cardNo   卡号
	 * @param cardType 卡类型
	 * @param topClass topClass
	 * @return 卡品牌
	 */
	public static Integer determineCardBrand(String cardNo, Integer cardType, String topClass) {
		//交通发行
		boolean trafficIssue = "6101".equals(topClass) || "6102".equals(topClass) || "6121".equals(topClass);
		if (trafficIssue) {
			if (cardType.equals(23)) {
				//握奇
				return 2;
			}
			long number = Long.parseLong(cardNo.substring(8, 20));
			if (number > 220000000001L && number < 220000431000L) {
				// 握奇
				return 2;
			}
			if ((number > 220000431001L && number < 220000461000L) || number >= 220005000001L) {
				//楚天龙
				return 5;
			}
		}

		//民生、招行、光大、华夏
		if ("6115".equals(topClass) || "6111".equals(topClass) || "6113".equals(topClass) || "6119".equals(topClass)) {
			//握奇
			return 2;
		}

		//农行
		if ("6121".equals(topClass)) {
			//天喻
			return 3;
		}
		//中行、邮储
		if ("6109".equals(topClass) || "6103".equals(topClass)) {
			return 9;
		}

		//西安银行
		if ("6117".equals(topClass)) {
			//金邦达
			return 6;
		}

		//默认握奇
		return 2;
	}

	/**
	 * 确定部中心规定的卡片类型
	 *
	 * @param topClass topClass
	 * @param cardType 卡类型
	 * @return 新类型
	 */
	public static Integer determineCardType(String topClass, Integer cardType) {
		StringBuilder newType = new StringBuilder();
		newType.append(cardType.equals(22) ? "2" : "1");
		//交通发行
		boolean trafficIssue = "6101".equals(topClass) || "6102".equals(topClass) || "6120".equals(topClass);
		if (trafficIssue) {
			newType.append("11");
			//民生银行 借记卡+二卡分离
		} else if ("6115".equals(topClass)) {
			newType.append("32");
			//农业银行 贷记卡+二卡分离
		} else if ("6111".equals(topClass)) {
			newType.append("33");
		} else {
			newType.append("22");
		}
		return Integer.valueOf(newType.toString());
	}

	public static Integer determineCardType(Integer cardType, Integer bindAccountType, Integer bindCardType){
		if (cardType == null || bindAccountType == null || bindCardType == null){
			return null;
		}
		return Integer.valueOf((cardType == CardStaticValue.EcardType.CARD_TYPE_CZ ? 2 : 1) +StringUtils.EMPTY + bindAccountType + bindCardType);
	}

	/**
	 * OBU品牌
	 *
	 * @param contractNo obu合同号
	 * @param obuNo      obu编号
	 * @return obu品牌
	 */
	public static Integer determineObuBrand(String contractNo, String obuNo) {
		if (StringUtils.isBlank(contractNo)) {
			return 3;
		}
		String flag = contractNo.substring(8, 10);
		if ("0C".equals(flag)) {
			//握奇
			return 9;
		}
		if ("08".equals(flag)) {
			//万集
			return 7;
		}
		if ("03".equals(flag)) {
			//聚利
			return 3;
		}
		if ("61".equals(flag) || "02".equals(flag)) {
			//金溢
			return 2;
		}
		if ("07".equals(flag)) {
			//千方
			return 6;
		}
		if ("0D".equals(flag)) {
			//搜林
			return 10;
		}

		if (StringUtils.length(obuNo) == 15) {
			//聚利
			return 3;
		}

		if (StringUtils.length(obuNo) == 16) {
			String sign = obuNo.substring(8, 10);
			if ("01".equals(sign)) {
				// 聚利
				return 3;
			}
			if ("02".equals(sign)) {
				// 万集
				return 7;
			}
			if ("03".equals(sign)) {
				// 握奇
				return 9;
			}
		}
		//默认聚利
		return 3;
	}

	public static String determineObuModel(Integer obuBrand, String obuNo) {
		if (obuBrand == null || obuBrand == 0) {
			obuBrand = 3;
		}
		//聚利
		if (obuBrand == 3) {
			return "JLCZ-06";
		}
		//握奇
		if (obuBrand == 9) {
			return "Quipass016";
		}
		//万集
		if (obuBrand == 7 && StringUtils.length(obuNo) == 16) {
			String subNo = obuNo.substring(7, 16);
			long subNoVal = Long.parseLong(subNo);
			return subNoVal >= 200040001L && subNoVal <= 200084000 ? "W-116" : "WE011-01";
		}
		return "JLCZ-06";
	}


	public static void main(String[] args) {
		System.out.println(determineCardType("6101", 22));
	}
}
